<?php

class sbPrices {

    public function hello() {
        return 'hello';
    }

    public function getPrices($hotelId, $roomId, $from, $to) {
        return Doctrine_Query::create()->
                        select('*')->
                        from('tblPrice')->
                        where('hotel_id=? AND room_id=? AND date BETWEEN ? AND ?', array($hotelId, $roomId, $from, $to))->
                        execute();
    }

    public function createStringDescription($hotelId, $roomId, $from, $to) {
        $hotel = tblHotelTable::getInstance()->findByDql('id=?', $hotelId)->getFirst();
        $room = tblRoomsTable::getInstance()->findByDql('id=?', $roomId)->getFirst();

        return 'Hotel: \'' . $hotel->getNameEn() . '\' Room: \'' . $room->getName() . '\' Date: ' . $from . ' --- ' . $to;
    }

    public function updateCurrentPrice($dPrice) {
        $price = tblPriceTable::getInstance()->findByDql('id=?', $dPrice['id'])->getFirst();
        if ($price) {
            $price->setFitPriceUsd($dPrice['fit_usd']);
            $price->setFitPriceNis($dPrice['fit_nis']);
            $price->setCascadePriceUsd($dPrice['cascade_usd']);
            $price->setCascadePriceNis($dPrice['cascade_nis']);
            $price->setSpecialPriceUsd($dPrice['special_usd']);
            $price->setSpecialPriceNis($dPrice['special_nis']);
            $price->setRussianMarketUsd($dPrice['market_usd']);
            $price->setUpdateTime(sbValues::getCurrentTime());
            $price->save();

            return 'success';
        } else {
            return 'not found';
        }
    }

    public function updatePeriodPrices($dPrice, $days, $period, $hotelId, $roomId, $from, $to) {
        $arrDays = $this->createDateRangeArray($from, $to);

        switch ($days) {
            case sbValues::ALL_DAYS:
                $result = $this->updateAllDaysPrice($hotelId, $roomId, $period, $dPrice, $arrDays);
                break;
            case sbValues::WORKING_DAYS:
                $result = $this->updateWorksDaysPrice($hotelId, $roomId, $period, $dPrice, $arrDays);
                break;
            case sbValues::WEKEEND:
                $result = $this->updateWekeendsDaysPrice($hotelId, $roomId, $period, $dPrice, $arrDays);
                break;
        }

        return $result;
    }

    public function updateWekeendsDaysPrice($hotelId, $roomId, $period, $dPrice, $arrDays) {
        foreach ($arrDays as $day) {
            $day_index = date('w', strtotime($day));
            if ($day_index == 5 || $day_index == 6) {
                $price = tblPriceTable::getInstance()->findByDql('room_id=? AND date=? AND period=?', array($roomId, $day, $period))->getFirst();
                if ($price) {
                    $this->updatePrice($price, $dPrice);
                } else {
                    $this->createPrice($day, $roomId, $hotelId, $period, $dPrice);
                }
            }
        }

        return 'Success';
    }

    public function updateWorksDaysPrice($hotelId, $roomId, $period, $dPrice, $arrDays) {
        foreach ($arrDays as $day) {
            $day_index = date('w', strtotime($day));
            if ($day_index != 5 && $day_index != 6) {
                $price = tblPriceTable::getInstance()->findByDql('room_id=? AND date=? AND period=?', array($roomId, $day, $period))->getFirst();
                if ($price) {
                    $this->updatePrice($price, $dPrice);
                } else {
                    $this->createPrice($day, $roomId, $hotelId, $period, $dPrice);
                }
            }
        }

        return 'Success';
    }

    public function updateAllDaysPrice($hotelId, $roomId, $period, $dPrice, $arrDays) {
        foreach ($arrDays as $day) {
            $price = tblPriceTable::getInstance()->findByDql('room_id=? AND date=? AND period=?', array($roomId, $day, $period))->getFirst();
            if ($price) {
                $this->updatePrice($price, $dPrice);
            } else {
                $this->createPrice($day, $roomId, $hotelId, $period, $dPrice);
            }
        }

        return 'Success';
    }

    public function updatePrice($price, $dPrice) {
        $price->setFitPriceUsd($dPrice['fit_usd']);
        $price->setFitPriceNis($dPrice['fit_nis']);
        $price->setCascadePriceUsd($dPrice['cascade_usd']);
        $price->setCascadePriceNis($dPrice['cascade_nis']);
        $price->setSpecialPriceUsd($dPrice['special_usd']);
        $price->setSpecialPriceNis($dPrice['special_nis']);
        $price->setRussianMarketUsd($dPrice['market_usd']);
        $price->setUpdateTime(sbValues::getCurrentTime());
        $price->save();

        return true;
    }

    public function createPrice($date, $roomId, $hotelId, $period, $dPrice) {
        $price = new tblPrice();
        $price->setHotelId($hotelId);
        $price->setRoomId($roomId);
        $price->setDate($date);
        $price->setPeriod($period);
        $price->setFitPriceUsd($dPrice['fit_usd']);
        $price->setFitPriceNis($dPrice['fit_nis']);
        $price->setCascadePriceUsd($dPrice['cascade_usd']);
        $price->setCascadePriceNis($dPrice['cascade_nis']);
        $price->setSpecialPriceUsd($dPrice['special_usd']);
        $price->setSpecialPriceNis($dPrice['special_nis']);
        $price->setRussianMarketUsd($dPrice['market_usd']);
        $price->setUpdateTime(sbValues::getCurrentTime());
        $price->setCreateTime(sbValues::getCurrentTime());
        $price->save();

        return true;
    }

    function createDateRangeArray($strDateFrom, $strDateTo) {
        $aryRange = array();

        $iDateFrom = mktime(1, 0, 0, substr($strDateFrom, 5, 2), substr($strDateFrom, 8, 2), substr($strDateFrom, 0, 4));
        $iDateTo = mktime(1, 0, 0, substr($strDateTo, 5, 2), substr($strDateTo, 8, 2), substr($strDateTo, 0, 4));

        if ($iDateTo >= $iDateFrom) {
            array_push($aryRange, date('Y-m-d', $iDateFrom)); // first entry
            while ($iDateFrom < $iDateTo) {
                $iDateFrom+=86400;
                array_push($aryRange, date('Y-m-d', $iDateFrom));
            }
        }
        return $aryRange;
    }

}
